<template>
    <el-container>
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img class="ng_logo" src="../../assets/img/ng_logo_tm.png" alt="">
                <span>高线厂辊环管理系统</span>
            </div>
            <div>
             <el-badge :value="12" class="item">
                <i class="el-icon-bell"></i> 
            </el-badge>              
              <img class="jh_logo" src="../../assets/img/jhLogo.png" alt=""><p class="jh_logo_text">江苏金恒信息科技股份有限公司</p>          
              <el-menu  mode="horizontal" 
                        @select="handleSelect" 
                        background-color="#373d41"
                        text-color="#fff"
                        active-text-color="#fff"
                        >
                <el-submenu index="1" >
                    <template slot="title">
                        <i class="el-icon-user"></i>{{this.$store.state.username}}         
                    </template>
                    <el-menu-item index="1"><i class="el-icon-star-off"/>基本资料</el-menu-item>
                    <el-menu-item index="2"><i class="el-icon-edit"/>修改密码</el-menu-item>
                    <el-menu-item index="3"><i class="el-icon-close"/>退出</el-menu-item>    
                </el-submenu>
              </el-menu>
            </div>           
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 左边菜单栏 -->
            <el-aside :width="isCollapse ? '64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse"> <i :class="collapseImage"></i></div>
                <!-- 侧边栏区域 -->
                <el-menu background-color="#333744"
                text-color="#fff"
                active-text-color="#409bff"
                :unique-opened="true"
                :collapse="isCollapse"
                :collapse-transition = "false"
                router
                :default-active="activePath"
                >
                    <div v-for="item1 in menuList" :key="item1.id">         
                        <!-- 没有子菜单时 -->
                        <el-menu-item :index="'/'+item1.url" @click="saveNavState('/'+item1.path)" v-if="isFirstMenu(item1)">
                            <template slot="title" >
                                <i :class="item1.icon"></i>
                                <span slot="title">{{item1.name}}</span>
                            </template>                          
                        </el-menu-item>
                        <!-- 有子菜单时 -->
                         <!-- 一级菜单 -->
                        <el-submenu :index="'/'+item1.url" v-else>
                            <template slot="title">
                                <i :class="item1.icon"></i>
                                <span>{{item1.name}}</span>
                            </template>                                         
                            
                            <!-- 二级菜单 -->
                            <el-menu-item :index="'/'+item2.url" v-for="item2 in item1.node" :key="item2.id" @click="saveNavState('/'+item2.url)">
                                <template slot="title" >
                                    <i :class="item2.icon"></i>
                                    <span slot="title">{{item2.name}}</span>
                                </template>                          
                            </el-menu-item>                        
                        </el-submenu>
                    </div>   
                </el-menu>
            </el-aside>
            <!-- 主画面 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
    name: 'Home',
    data () {
        return {
            menuList: [],
            iconsObj: {
                125: 'el-icon-user',
                103: 'el-icon-lock',
                101: 'el-icon-present',
                102: 'el-icon-shopping-cart-full',
                145: 'el-icon-pie-chart'
            },
            isCollapse: false,
            collapseImage: 'el-icon-s-fold',
            // 被激活的链接地址
            activePath: ''
        }
    },
    watch: {},
    created () {
        this.getUser()
        this.getMenuList()       
    },
    methods: {
        // 退出
        logout () {
            // 清空token
            window.sessionStorage.clear()         
            this.$router.push('/login')
        },
        // 获取菜单项
        async getMenuList () {
           await this.$http.get('/sys/menu/listTree').then(({ data:res })=>{               
                if (res.code !== 2000) {
                    return this.$message.error(res.msg)
                }else{
                    this.menuList = res.menu
                }                
           }).catch(error=>{
               this.$message.error(error)
           })
        },
        // 点击按钮 切换菜单的折叠与展开
        toggleCollapse () {
            this.isCollapse = !this.isCollapse
            if (this.isCollapse) this.collapseImage = 'el-icon-s-unfold'
             else this.collapseImage = 'el-icon-s-fold'
        },
        // 保存链接的激活状态
        saveNavState (activePath) {
            window.sessionStorage.setItem('activePath', activePath)
            this.activePath = window.sessionStorage.getItem('activePath')
        },
        handleSelect(key){
            if (key === '1'){
                console.log(key)
            } else if (key === '2'){
                 console.log(key)
            } else {
                this.logout()  //退出
            }
        },
        getUser(){           
            const username = window.sessionStorage.getItem('username')   
            this.$store.commit('setUsername',{username})  //存储登录状态
        },
        // 判断是否显示为一级菜单
        isFirstMenu(item) {
            let flag = true
            const node = item.node
            if(node.length !== 0){ 
              node.forEach(element => {                                      
                    if (element.type === '1') {
                        flag = false
                    }
                })    
            }
            return flag
        }
    }
}
</script>

<style scoped>
.el-container{
    height: 100%;
}
.el-header {    
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
     padding-left: 0;
     align-items: center;   /* 按钮居中 */
     color: #fff;
     font-size: 20px;
  }

  .ng_logo {
      width: 40px;
      height: 40px;
      margin-left: 20px;
  }
  .jh_logo {
      width: 30px;
      height: 30px;            
  }
  .jh_logo_text{
      font-size: 12px;
      }
  .el-header span {
          margin-left: 20px;
      }

.el-header div{
    display: flex;
    align-items: center;
  }

  .el-aside {
    background-color: #333744;
  }

  .el-menu{
      border-right: none;
  }

  .el-main {
    background-color: #eaedf1;
  }

  .toggle-button{
      background-color: #4a5064;
      font-size: 20px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      cursor: pointer;
  }
  .item{
      margin-right: 30px;
  }
</style>
